/*
 * @Description:
 * @Author: xuzp4
 * @Date: 2022-03-22 08:54:13
 * @LastEditors: xuzp4
 * @LastEditTime: 2022-08-15 21:41:28
 * @FilePath: \visitor_wechat\src\services\index.js
 */
import axios from 'axios'
import store from '../store/index'

/** 员工接口 */

// 员工——接口请求统一封装
function httpRequest ({url, method = 'post', params = {}}) {
  const tokenVal = store.state.token
  // const sign = signMd5Utils.getSign(url, params)
  const configParam = {
    url,
    method,
  }
  if (method.toLowerCase() === 'get') {
    configParam.params = params
  } else if (method.toLowerCase() === 'postquery') {
    configParam.method = 'post'
    configParam.params = params
  } else {
    configParam.data = {
      ...params
    }
  }
  if (!tokenVal) {
  } else {
    configParam.headers['X-Access-Token'] = tokenVal
  }
  return axios(configParam).then(res => {
    if (res.status === 200) {
      return res.data
    } else if (res.status === 401) {
      // getTokenServer().then(() => {
      //   return httpRequest({ url, method, params })
      // })
    } else {
      throw res
    }
  }).catch(error => {
    if (error.message.indexOf('401') !== -1) {
      sessionStorage.clear()
      localStorage.clear()
      // const currentTime = new Date().getTime();
      // const oauthUri = `${getSsoLoginUri()}?clientId=${getSsoLoginClientId()}&redirectUri=${getSsoRedirectUri()}&state=${currentTime}`;
      // location.href = oauthUri
    } else {
      throw error
    }
  })
}

// 获取token信息
// export function getTokenServer (ticket) {
//   const url = `${getTokenApi}?ticket=${ticket}`
//   const reqParam = {
//     url,
//     method: 'post',
//     headers: {
//       'X-sign': signMd5Utils.getSign(url),
//       'X-TIMESTAMP': signMd5Utils.getDateTimeToString()
//     }
//   }
//   return axios(reqParam).then(res => {
//     const resObj = res.data || {}
//    if (res.status === 200 && resObj.code === 200) {
//       const resData = res.data.result || {}
//       const mapData = resData.sysAllDictItems || {}
//       const purposeList = mapData.mhy_visit_purpose || []
//       const refuseList = mapData.mhy_refuse_reason || []
//       store.dispatch('asyncSetToken', resData.token || '')
//       store.dispatch('asyncSetUserInfo', resData.userInfo || {})
//       store.dispatch('asyncSetRefuseReason', refuseList)
//       store.dispatch('asyncSetVisitorPurpose', purposeList)
//       return resData.token
//     }
//     else if(res.status === 401){
//       sessionStorage.clear()
//       this.$router.push("/")
//     }
//     else {
//       throw res
//     }
//   }).catch(res => {
//     throw res
//   })
// }

// 获取来访地址list
// export function getAddressTreeServer (params) {
//   return httpRequest({
//     url: addressTreeApi,
//     method: 'get',
//     params
//   })
// }
